package com.shequan.demo.service.impl;

import java.util.List;

import org.apache.log4j.Logger;

import com.shequan.common.bean.ResultDataStruct;
import com.shequan.demo.bean.pojo.DemoUserInfo;
import com.shequan.demo.dao.DemoDAO;
import com.shequan.demo.service.DemoService;

public class DemoServiceImpl implements DemoService {
	
	private static Logger log = Logger.getLogger(DemoServiceImpl.class);

	private DemoDAO demoDAO;

	public void setDemoDAO(DemoDAO demoDAO) {
		this.demoDAO = demoDAO;
	}

	public DemoUserInfo getUserInfo4Login(String userName, String password) throws Exception {
		DemoUserInfo tmp = new DemoUserInfo();
		tmp.setUserName(userName);
		tmp.setUserPwd(password);
		DemoUserInfo user = demoDAO.selectUserByUserNameAndUserPwd(tmp);
		return user;
	}
	
	public DemoUserInfo getUserInfoByUserName(String userName) throws Exception {
		DemoUserInfo tmp = new DemoUserInfo();
		tmp.setUserName(userName);
		return demoDAO.selectUserByUserName(tmp);
	}
	
	public ResultDataStruct<Integer> registUser(String userName, String userPwd) throws Exception {
		log.debug(userName+" "+userPwd);
		ResultDataStruct<Integer> rds=new ResultDataStruct<Integer>();
		if(getUserInfoByUserName(userName)!=null){
			rds.setBusiSuccess(false);
			rds.setMessage("用户名已存在");
		}else{
			DemoUserInfo user=new DemoUserInfo();
			user.setUserName(userName);
			user.setUserPwd(userPwd);
			int id=demoDAO.insert(user);
			log.debug("id:"+id);
			if(id>0){
				rds.setBusiSuccess(true);
				rds.setData(id);
				rds.setMessage("注册成功");
			}else{
				rds.setBusiSuccess(false);
				rds.setMessage("注册失败");
			}
		}
		return rds;
	}

	public List<DemoUserInfo> getAllUsers() throws Exception {
		return demoDAO.selectAllUsers();
	}

}
